//
//  HWTakeALookDetailCommentCell.swift
//  PlayWithProject
//
//  Created by 王昌健 on 2021/8/4.
//  动态详情评价列表cell

import UIKit

class HWTakeALookDetailCommentCell: UICollectionViewCell {
    var headImageView:UIImageView!
    var headFrameImageView:UIImageView!
    var nameLabel:GranibleLabel!
    var contentLabel:UILabel!
    var zanButton:UIButton!
    var zanLabel:UILabel!
    var timeLabel:UILabel!
    
    var name:String = ""
    
    var zanButtonClick:((_:String)->())?
    var model:HWTakeALookCommentModel?{
        didSet{
//            nameLabel.removeFromSuperview()
//            nameLabel.font = .boldSystemFont(ofSize: 14)
//            nameLabel.isUserInteractionEnabled = true
//            contentView.addSubview(nameLabel)
            
            headImageView.sd_setImage(with: URL(string: model?.face ?? ""), completed: nil)
            nameLabel.text = model?.pet_name
            zanLabel.text = model?.zan
            timeLabel.text = model?.time
            contentLabel.text = model?.content

            
            if model?.is_zan == true {
                zanButton.setImage(UIImage(named: "commentZanSelect"), for: UIControl.State.normal)
            }else{
                zanButton.setImage(UIImage(named: "commentZanNormal"), for: UIControl.State.normal)
            }
            
            headFrameImageView.sd_setImage(with: URL(string: model?.head_frame ?? ""), completed: nil)
//            if model?.head_frame.count ?? 0 > 0{
//                headImageView.snp.remakeConstraints { make in
//                    make.left.equalTo(20)
//                    make.top.equalTo(80)
//                    make.width.height.equalTo(20)
//                }
//                nameLabel.snp.remakeConstraints { make in
//                    make.left.equalTo(headFrameImageView.snp.right).offset(14)
//                    make.top.equalTo(headImageView)
//                }
//
//
//            }else{
//                headImageView.snp.makeConstraints { make in
//                    make.top.equalTo(10)
//                    make.left.equalTo(70)
//                    make.width.height.equalTo(20)
//                }
//
//                nameLabel.snp.makeConstraints { make in
//                    make.left.equalTo(headImageView.snp.right).offset(14)
//                    make.centerY.equalTo(headImageView)
//                }
//
//            }
            
            if (model?.color.count ?? 0) > 1 {
                var arr:[UIColor] = Array()
                for color in model?.color ?? [] {
                    arr.append(UIColor.colorWithHexString(hexString: color))
                }
                nameLabel.colors = arr
            }else {
                nameLabel.colors = [UIColor.colorWithHexString(hexString: model?.color[0] ?? "#666666"),UIColor.colorWithHexString(hexString: model?.color[0] ?? "#666666")]
            }

        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
extension HWTakeALookDetailCommentCell{
    private func initUI() -> Void {
        
        headImageView = UIImageView()
        headImageView.backgroundColor = UIColor.clear
        headImageView.layer.cornerRadius = 10
        headImageView.layer.masksToBounds = true
        headImageView.isUserInteractionEnabled = true
        contentView.addSubview(headImageView)
        
        headFrameImageView = UIImageView()
        headFrameImageView.backgroundColor = .clear
        headFrameImageView.layer.masksToBounds = true
        headFrameImageView.contentMode = .scaleAspectFit
        contentView.addSubview(headFrameImageView)
        
        nameLabel = GranibleLabel()
        nameLabel.font = .boldSystemFont(ofSize: 14)
        nameLabel.isUserInteractionEnabled = true
        contentView.addSubview(nameLabel)
        
        zanButton = UIButton.init(type: UIButton.ButtonType.custom)
        zanButton.addTarget(self, action: #selector(self.zanButtonClick(_:)), for: UIControl.Event.touchUpInside)
        zanButton.setImage(UIImage(named: "commentZanNormal"), for: UIControl.State.normal)
        zanButton.isUserInteractionEnabled = true
        contentView.addSubview(zanButton)
        
        zanLabel = HWLabel.createLabel(title: "", color: "#999999", font: 10)
        contentView.addSubview(zanLabel)
        
        contentLabel = HWLabel.createLabel(title: "", color: "#333333", font: 13)
        contentView.addSubview(contentLabel)
        
        timeLabel = HWLabel.createLabel(title: "-", color: "#999999", font: 10)
        contentView.addSubview(timeLabel)
    }
    
    private func initFrame() -> Void {
        contentView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.width.equalTo(screenWidth)
        }
        headImageView.snp.makeConstraints { make in
            make.top.equalTo(10)
            make.left.equalTo(70)
            make.width.height.equalTo(20)
        }
        
        headFrameImageView.snp.makeConstraints { make in
            make.center.equalTo(headImageView)
            make.height.width.equalTo(30)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(headImageView.snp.right).offset(14)
            make.centerY.equalTo(headImageView)
        }
        
        zanButton.snp.makeConstraints { make in
            make.right.equalTo(-15)
            make.top.equalTo(contentLabel)
        }
        
        zanLabel.snp.makeConstraints { make in
            make.centerX.equalTo(zanButton)
            make.top.equalTo(zanButton.snp.bottom)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(headImageView.snp.right).offset(14)
            make.top.equalTo(headImageView.snp.bottom).offset(10)
            make.right.equalTo(zanButton.snp.left).offset(-30)
//            make.bottom.equalTo(-10)
        }
        
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp.bottom).offset(5)
        }
        
        zanButton.setContentHuggingPriority(UILayoutPriority.required, for: NSLayoutConstraint.Axis.horizontal)
    }
}

extension HWTakeALookDetailCommentCell {
    @objc func zanButtonClick(_ button:UIButton) -> Void {
        zanButtonClick?(model?.id ?? "")
    }
}
